<template>
  <div class="box-card">
    <div slot="header" class="cardHeader">
      <span class="headSpan">流程分类</span>
      <el-button class="headBtn" type="text" @click="handleAdd()">+ 新增{{ name }}</el-button>
    </div>
    <el-input
      v-model.trim="filterText"
      size="small"
      prefix-icon="el-icon-search"
      placeholder="请输入名称"
      maxlength="30"
      style="margin: 16px 0 0 0"
    />
    <div class="left-title" />
    <el-tree
      ref="tree"
      empty-text="暂无数据"
      :style="{ '--lightOpacity': lightTheme }"
      class="left-tree"
      :data="tree"
      :props="defaultProps"
      :default-expand-all="false"
      :expand-on-click-node="false"
      :highlight-current="true"
      :current-node-key="currentKey"
      :filter-node-method="filterNode"
      node-key="categoryId"
      :default-expanded-keys="expendData"
      @node-click="handleNodeClick"
    >
      <div slot-scope="{ node, data }" class="custom-tree-node">
        <svg-icon icon-class="organization" />
        <DtOverTooltip :content="data.categoryName" />
        <template>
          <el-dropdown v-if="data.categoryId != '-1' && data.categoryKey !='lowcode'">
            <span @click.stop>
              <svg-icon class="el-dropdown-link" icon-class="sandian" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(data)">
                编辑
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDel(data)">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </div>
    </el-tree>
    <classifiyEdit ref="adminGroupRef" :cascader-opts="tree" />
  </div>
</template>
<script>
import classifiyEdit from './classifiyEdit.vue'
import processClassifiy from '@/framework/api/processDesign/processClassifiy.js'

export default {
  name: 'AdminGroup',
  components: { classifiyEdit },
  props: {
    name: {
      type: String,
      default: ''
    },
    handle: {
      type: Boolean,
      default: false
    },
    defaultType: {
      type: String,
      default: '-1'
    }
  },
  data() {
    return {
      selectedOrg: '',
      orgName: '',
      filterText: '',
      tree: [],
      newTree: [],
      defaultProps: {
        children: 'children',
        label: 'categoryName'
      },
      currentKey: '',
      expendData: []
    }
  },
  computed: {
    lightTheme() {
      return `${this.$store.state.settings.theme}19`
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.initTree()
  },
  methods: {
    /**
     * 选中树节点
     */
    handleNodeClick(data) {
      this.selectedOrg = data
      this.$emit('treeClick', data.categoryId === '-1' ? '' : data.categoryId, data.categoryKey)
    },
    // 获取左侧组织树
    initTree(val) {
      processClassifiy.getGroupTree().then((res) => {
        if (!res.data.length) {
          this.tree = []
          this.$emit('treeClick')
          return
        }
        this.tree = [
          {
            categoryName: '全部',
            categoryId: '-1',
            children: res.data
          }
        ]
        const curRow = val || { categoryId: this.defaultType || '-1' }
        this.selectedOrg = curRow
        this.expendData = [curRow.categoryId]
        this.$emit('treeClick', curRow.categoryId === '-1' ? '' : curRow.categoryId)
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(curRow.categoryId)
        })
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.categoryName.indexOf(value) !== -1
    },
    handleAdd() {
      this.$refs.adminGroupRef.openOrg('', 'create', this.selectedOrg)
    },
    handleEdit(data) {
      this.$refs.adminGroupRef.openOrg(data, 'update')
    },
    handleDel(data) {
      this.$dtMessageBox({
        title: '提示',
        message: `确定要删除“${data.categoryName}”吗?`,
        type: 'warning'
      }).then(() => {
        processClassifiy
          .delGroup({ categoryId: data.categoryId })
          .then((res) => {
            if (res.code === '00000') {
              if (this.selectedOrg.categoryId === data.categoryId) {
                this.initTree({ categoryId: '-1' })
              } else {
                this.initTree(this.selectedOrg)
              }
              this.$dtMessage({
                message: '删除成功',
                type: 'success'
              })
            }
          })
          .catch(() => {
            // 取消不做操作
          })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.cardHeader {
  .headSpan {
    font-size: 16px;
    font-family: HarmonyOS Sans SC-Medium, HarmonyOS Sans SC;
    font-weight: 500;
    color: #131414;
    line-height: 24px;
  }

  .headBtn {
    float: right;
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    line-height: 24px;
    padding: 0;
    font-weight: 400;
  }
}

.svg-icon {
  font-size: 24px;
  display: block;
  margin-right: 8px !important;
  float: none !important;
}

.left-title {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 16px;
}

.left-tree {
  margin-top: 23px;
}

::v-deep .el-card__header {
  padding: 0;
  border-bottom: none;
}

::v-deep .el-card__body {
  padding: 0;
}

::v-deep .el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {
  background-color: var(--lightOpacity);
  border-radius: 4px;
  color: var(--themeColor);

  .el-dropdown-link {
    color: var(--themeColor);
  }
}

::v-deep .el-tree-node__content:hover,
.el-upload-list__item:hover {
  background-color: var(--lightOpacity);
  border-radius: 4px;
  // color: var(--themeColor);
}

::v-deep .el-tree-node__content {
  overflow: hidden;
  height: 38px;
}

::v-deep .el-dropdown-menu__item {
  padding: 6px 16px !important;
  line-height: 24px;
}

.custom-tree-node {
  display: flex;
  align-items: center;
  overflow: hidden;
  width: 100%;

  .tree-name {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    flex: 1;
  }

  .btn {
    color: var(--themeColor);

    i {
      margin-right: 8px;
      font-size: 14px;
    }
  }

  .el-dropdown-link {
    margin-right: 8px;
    display: block;
    // width: 16px;
    // height: 16px;
    text-align: center;
    font-size: 14px;
  }

  .el-dropdown-link:hover {
    color: var(--themeColor);
  }
}
</style>
